﻿#include "Game.h"

#include "_Global/Socket/ClientSocket.h"



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//////////////////////Game/////////////////////////////////////

Game::Game(QObject *parent)
    :GameCore(parent)
{

    //m_mutexUserAction.tryLock();
    //m_mutexQuestion.tryLock();
    //m_mutexNet.tryLock();


    //!!加牌!!
    /*
    for(n = GAME_MAX_NUM_PAI; i < n; i++)   //桃
    {
        m_PaiDui[i].setProp(i,POKE_TYPE_JIBEN,_Pai::Value_Tao);
        m_PaiDui[i].pai = &pai_Tao;
    }*/

//    for(n += 30;i < n;i++)  //杀
//    {
//        m_PaiDui[i].setProp(i,POKE_TYPE_JIBEN,_Pai::Value_Sha);
//        m_PaiDui[i].pai = &pai_Sha;
//    }
//    for(n = GAME_MAX_NUM_PAI;i < n;i++)   //碳素笔
//    {
//        m_PaiDui[i].setProp(i,POKE_TYPE_ZHUANGBEI,_Pai::Value_TanSu);
//        m_PaiDui[i].pai = &pai_TanSu;
//    }

    /*
    for(n+=10;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=4;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=3;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=2;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=1;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha309;
    for(n+=1;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=1;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=1;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=2;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=6;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=8;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=3;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=2;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=1;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=2;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=3;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=2;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;



    m_WuJiang[0].iType = _Pai::Type_WuJiang;
    m_WuJiang[0].iValue = VALUE_WUJIANG_LIUSHUAI;
    m_WuJiang[0].tilishangxian = 4;
    m_WuJiang[1].iType = _Pai::Type_WuJiang;
    m_WuJiang[1].iValue = VALUE_WUJIANG_SUM;
    m_WuJiang[1].tilishangxian = 4;
    / *
    pai_Sha.setProp(POKE_TYPE_JIBEN,_Pai::Value_Sha,"Sha.bmp");
    pai_Shan.setProp(POKE_TYPE_JIBEN,_Pai::Value_Shan,"Shan.bmp");
    pai_Tao.setProp(POKE_TYPE_JIBEN,_Pai::Value_Tao,"Tao.bmp");
    pai_ShanDian.setProp(POKE_TYPE_JINNANG1,_Pai::Value_ShanDian,"ShanDian.bmp");
    pai_Shun.setProp(POKE_TYPE_JINNANG0,_Pai::Value_Shun,"Shun.bmp");
    pai_Chai.setProp(POKE_TYPE_JINNANG0,_Pai::Value_Chai,"Chai.bmp");

    pai_ZhangBa.setProp(POKE_TYPE_WUQI,_Pai::Value_ZhangBa,"ZhangBa.bmp");
    pai_QingLong.setProp(POKE_TYPE_WUQI,_Pai::Value_QingLong,"QingLong.bmp");

    Button_OK.setProp(POKE_TYPE_NULL,POKE_TYPE_NULL,"Button_OK.bmp");
    Button_Cancel.setProp(POKE_TYPE_NULL,POKE_TYPE_NULL,"Button_Cancel.bmp");
    Button_Giveup.setProp(POKE_TYPE_NULL,POKE_TYPE_NULL,"Button_Giveup.bmp");
    */
}

QObject* Game::wordLib() const
{
    return m_pWordLib;
}

int Game::InitOnce()
{
    m_Speech.engineSelect(0);
    m_Speech.locateSelect(0);
    m_Speech.voiceSelect(0);

    return GameCore::InitOnce();
}
int Game::InitOnceInThread()
{
    return GameCore::InitOnceInThread();
}

int Game::GameInit(int sr)
{
    //m_mutexNet.tryLock();
    //releaseDataList();

    m_Producer.init();

    return GameCore::GameInit(sr);
}


/*
    if(pData->type == GAME_DATA_TRANSFER_XIPAI)
    {
        memcpy(m_arrayPaiDuiID,pData->ChuPaiData.data,pData->ChuPaiData.nDataLength);
        RefreshPaiDui();
        m_nPaiDuiShu = pData->ChuPaiData.nData;

        delete pData;
        return 0;
    }
    else if(pData->type == GAME_DATA_TRANSFER_CHUPAI)
    {
        memcpy(step,pData->ChuPaiData.data,pData->ChuPaiData.nDataLength);
        int ret = pData->ChuPaiData.nData;
        delete pData;

        return ret;
    }
    else
    {
        qDebug()<<"WaitingForOthers Error!"<<pData->type;
        return -1;
    }
    return -1;
    */

/*
void Game::SendToOthers(int iType)
{
    switch(iType)
    {
    case GAME_DATA_TRANSFER_XIPAI:
        {
            m_pGameSocket->sendData()<<(qint16)GAME_DATA<<(qint16)GAME_DATA_TRANSFER_XIPAI
                            <<(qint16)m_nPaiDuiShu
                            <<(qint16)(sizeof(m_arrayPaiDuiID));
            m_pGameSocket->sendData().writeRawData((char*)m_arrayPaiDuiID,sizeof(m_arrayPaiDuiID));
        }
        break;
    }
    SendDataToServer();
}
*/

GameCore::_GameStatus Game::ChoiceWuJiang(int count)
{
    GameCore::ChoiceWuJiang(count);
    if(gameStatus() != Game_Status_Continue)return gameStatus();

    //电脑 随机选择武将
    if(m_bNetPlay == false)
    {
        int i, j = 0;
        qint32 temp[GAME_MAX_NUM_WUJIANG];

        //!!!这里可以修改为所有武将中挑选
        //if(!GetDifferentNumber(0,GAME_MAX_NUM_WUJIANG,temp,count))
        if(!GetDifferentNumber(1,GAME_MAX_NUM_WUJIANG + 1,temp,GAME_MAX_NUM_WUJIANG,sizeof(qint32)))
        {
            emit s_Critical(_T("[!GameCore]随机范围 ERROR!"));
            return setGameStatus(Game_Status_ERROR);
        }
        //配置电脑武将(随机)
        for(i = 0; i < count; i++)
        {
            if(i == m_nMyID)
            {
                //m_arrayPlayer[m_nMyID].eJiaoSe = _Player::JiaoSe_Self;
                continue;
            }

            //跳过已选的
            while(m_mapWuJiangUsingTimes.contains(temp[j]))
            {
                j++;

                if(j >= GAME_MAX_NUM_WUJIANG)
                {
                    emit s_Critical(_T("[!GameCore]电脑选择人物 ERROR!"));
                    return setGameStatus(Game_Status_ERROR);
                }
            }

            m_mapWuJiangUsingTimes[temp[j]] = 1;

            m_arrayPlayer[i].getWuJiang((_WuJiang::_Value)temp[j]);
            //m_arrayPlayer[i].m_listWuJiangQu.at(0)->createWuJiang(temp[j]);
        }

        //delete temp;
    }
    else
    {
        //本人选择武将,发送数据
        QByteArray ba;
        QDataStream out(&ba,QIODevice::WriteOnly);
        out << (qint16)GAME_DATA << (qint16)GAME_DATA_TRANSFER_CHOICEWUJIANG
                        << (qint16)m_arrayPlayer[m_nMyID].getWujiangData()->id();
        SendDataToServer(ba);
        qDebug() << "[!!!选择武将:]" << (qint32)m_arrayPlayer[m_nMyID].getWujiangData()->id();

        emit s_Debug(_T("等待其他玩家选择武将:"));

        _GameData *pData = static_cast<_GameData*>(m_Producer.safeOut());
        if(CheckGameOver() != Game_Status_Continue)
        {
            if(pData != nullptr)delete pData;
            return setGameStatus(Game_Status_ERROR);
        }

        if(pData == nullptr || pData->type != GAME_DATA_TRANSFER_ALLWUJIANGCHOICED)
        {
            if(pData != nullptr)
            {
                emit s_Critical(_T("[!GameCore]选择武将同步 ERROR:%1!").arg(pData->type));
                delete pData;
            }
            else
                emit s_Critical(_T("[!GameCore]选择武将同步 ERROR:nullptr!"));
            return setGameStatus(Game_Status_ERROR);
        }

        delete pData;

        emit s_Debug(_T("选择武将接受ok"));
    }

    return gameStatus();
}

bool Game::Xipai()
{
    if(m_bMaster)
    {
        if(!GameCore::Xipai())
            return false;

        if(m_bNetPlay == true)  //这段用不到!!!服务器发送!!!
        {
            QByteArray ba;
            QDataStream out(&ba,QIODevice::WriteOnly);
            out << static_cast<qint16>(GAME_DATA) << static_cast<qint16>(GAME_DATA_TRANSFER_XIPAI)
                            << static_cast<qint16>(m_nPaiDuiShu)
                            << static_cast<qint16>(sizeof(m_arrayPaiDuiID));
            out.writeRawData(static_cast<const char *>(static_cast<void*>(m_arrayPaiDuiID)), sizeof(m_arrayPaiDuiID));
            SendDataToServer(ba);
            emit s_Debug(_T("洗牌发送数据:"));
        }
    }
    else
    {
        emit s_Debug(_T("等待洗牌:"));

        _GameData *pData = static_cast<_GameData*>(m_Producer.safeOut());
        if(CheckGameOver() != Game_Status_Continue)
        {
            if(pData != nullptr)delete pData;
            return false;
        }

        if(pData == nullptr || pData->type != GAME_DATA_TRANSFER_XIPAI)
        {
            if(pData != nullptr)
            {
                emit s_Critical(_T("[!GameCore]洗牌同步 ERROR:%1!").arg(pData->type));
                delete pData;
            }
            else
                emit s_Critical(_T("[!GameCore]洗牌同步 ERROR:nullptr!"));
            setGameStatus(Game_Status_ERROR);
            return false;
        }


        memcpy(m_arrayPaiDuiID, pData->XiPaiData.pData, (size_t)pData->XiPaiData.nDataLength);
        RefreshPaiDui();
        m_nPaiDuiShu = pData->XiPaiData.nData;

        delete pData;

        emit s_Debug(_T("洗牌接受ok"));
    }
    return true;
}

GameCore::_GameButtonValue Game::ChuPai(int player,_GameChuPaiType nType, int nValue, int nParam1, int nParam2, QVariant nParam3, int nInitStep, QString strMsg)
{
    _GameButtonValue ret = Game_Button_ERROR;

    startChuPai(player);

    //本人是主机,或网络游戏时轮自己出牌
    //if(m_arrayPlayer[player].isMe() || (m_bMaster == true && m_arrayPlayer[player].isComputer()))
    if(m_bMaster == true || (m_arrayPlayer[player].isMe() && m_bNetPlay == true))
    {
        ret = GameCore::ChuPai(player,nType,nValue,nParam1,nParam2,nParam3,nInitStep,strMsg);
        if(CheckGameOver() != Game_Status_Continue)
            return Game_Button_ERROR;

        /*
        //拆和顺，随机选择一张牌!!!!!!
        if(step[0].nSelectType[0] == _BaseObject::Area_ShouPai)
        switch(m_arrayPlayer[player].getShouPaiData(step[0].nSelectValue[0])->value())
        {
        case _Pai::Value_Chai:
        case _Pai::Value_Shun:
            {
                //step[2].nSelectType[0] = _BaseObject::Area_ShouPai;//!!!!!!
                //step[2].nSelectValue[0] = Random(0,m_arrayPlayer[step[1].nSelectPlayerID].m_listShouPai.count());
            }
            break;
        default:
            break;
        }*/

        if(m_bNetPlay == true)
        {
            //本人出牌,发送数据
            QByteArray ba;
            QDataStream out(&ba,QIODevice::WriteOnly);
            out<<(qint16)GAME_DATA<<(qint16)GAME_DATA_TRANSFER_CHUPAI
                            <<(qint16)ret<<(qint16)(sizeof(PokeStep) * m_nStepCount);
            out.writeRawData((const char*)m_arrayStep,(sizeof(PokeStep) * m_nStepCount));
            SendDataToServer(ba);
        }
    }
    else
    //(m_arrayPlayer[player].IsOtherPlayer() || (m_bMaster == false && m_arrayPlayer[player].IsComputer()))
    {
        //emit s_Debug(_T("等待对方出牌!"));
        ShowChuPaiMessage(_T("等待网络玩家出牌"), 0);

        _GameData *pData = (_GameData*)m_Producer.safeOut();
        if(CheckGameOver() != Game_Status_Continue)
        {
            if(pData != nullptr)delete pData;
            return Game_Button_ERROR;
        }

        if(pData == nullptr || pData->type != GAME_DATA_TRANSFER_CHUPAI)
        {
            if(pData != nullptr)
            {
                emit s_Critical(_T("[!GameCore]ChuPai同步 ERROR:%1!").arg(pData->type));
                delete pData;
            }
            else
                emit s_Critical(_T("[!GameCore]ChuPai同步 ERROR:nullptr!"));
            setGameStatus(Game_Status_ERROR);
            return Game_Button_ERROR;
        }


        memcpy(m_arrayStep,pData->ChuPaiData.pData,pData->ChuPaiData.nDataLength);
        ret = (_GameButtonValue)pData->ChuPaiData.nData;
        delete pData;

        //ShowChuPaiMessage(_T(""));
        emit s_Debug(_T("对方出牌接受OK:%1").arg(player));

    }
    stopChuPai(player);

    if(m_arrayPlayer[player].isComputer())
    {
        //if(nType == GameCore::Game_ChuPai_Type_Pai && nValue == _Pai::Value_Tao)
        //    ;
        //else
            //msleep(m_ComputerDelay);
    }
    return ret;
}


int Game::ShaiDian()
{
    qint32 n;
    if(m_bMaster)
    {
        n = GameCore::ShaiDian();

        if(m_bNetPlay == true)  //用不到!!
        {
            QByteArray ba;
            QDataStream out(&ba,QIODevice::WriteOnly);
            out<<(qint16)GAME_DATA<<(qint16)GAME_DATA_TRANSFER_RANDOM<<n;
            SendDataToServer(ba);
            emit s_Debug(_T("骰点发送数据:"));
        }
    }
    else
    {
        emit s_Debug(_T("等待骰点:"));

        _GameData *pData = (_GameData*)m_Producer.safeOut();
        if(CheckGameOver() != Game_Status_Continue)
        {
            if(pData != nullptr)delete pData;
            return -1;
        }

        if(pData == nullptr || pData->type != GAME_DATA_TRANSFER_RANDOM)
        {
            if(pData != nullptr)
            {
                emit s_Critical(_T("[!GameCore]ShaiDian同步 ERROR:%1!").arg(pData->type));
                delete pData;
            }
            else
                emit s_Critical(_T("[!GameCore]ShaiDian同步 ERROR:nullptr!"));
            setGameStatus(Game_Status_ERROR);
            return -1;
        }


        n = pData->nRandom;
        delete pData;

        emit s_Debug(_T("骰点接受ok"));
    }

    //n = n % 6;
    //Message(_T("筛到%1点!").arg(n));

    return n;
}


bool Game::WenTi(int player, QString msg, int time)
{
    bool answer = false;     //问题答案是否正确
    //return true;
    //_EnglishWord *pWord = nullptr;

    //ShowChuPaiMessage(msg);
    //msleep(m_ComputerDelay);
    //if(m_arrayPlayer[player].isMe() || (m_bMaster == true && m_arrayPlayer[player].isComputer()))
    if(m_bMaster == true)    //单人
    {

        /*
        if(m_bNetPlay == true)
        {
            m_pWordLib->getWord(&wordObj,false);
            QByteArray ba;
            QDataStream out(&ba,QIODevice::WriteOnly);
            out<<(qint16)GAME_DATA<<(qint16)GAME_DATA_TRANSFER_WENTI
                            <<wordObj.answer()
                            <<wordObj.answerChoice()
                            <<wordObj.word()
                            <<wordObj.phonetic()
                            <<wordObj.explain();

            SendDataToServer(ba);
        }
        */

        //如果是本人回答,且没有托管
        if(m_arrayPlayer[player].isMe() && !m_arrayPlayer[player].m_bTuoGuan)
        {
            if(!m_pWordLib->getWord(&wordObj,true))
            {
                emit s_Critical(_T("[!GameCore]getWord ERROR!"));
                setGameStatus(Game_Status_ERROR);
                return false;
            }
            ShowChuPaiMessage(msg + _T("，请选择一个正确答案"), 0);
            wordObj.m_nAnswerChoice = -1;
            emit s_Wenti(&wordObj,1,time);
            //RefreshUI();
            WaitForUserAction();
            if(CheckGameOver() != Game_Status_Continue)return false;

            //统计单词和对错
            if(wordObj.answerChoice() == wordObj.answer())
            {
                ContinualChoice(1,wordObj.choiceTime());
                m_pWordLib->m_strResult[m_pWordLib->m_nWordPointer] = 'y';
                //wordObj.pWord->myAnswer = 1;
            }
            else
            {
                ContinualChoice(0,0);
                m_pWordLib->m_strResult[m_pWordLib->m_nWordPointer] = 'n';
                //wordObj.pWord->myAnswer = 0;
            }
        }
        else    //COM
        {
            if(!m_pWordLib->getWord(&wordObj,false))
            {
                emit s_Critical(_T("[!GameCore]getWord ERROR2!"));
                setGameStatus(Game_Status_ERROR);
                return false;
            }
            //!!!跳过显示阶段
            ShowChuPaiMessage(msg + _T("，%1正在选择答案").arg(getPlayerShowName(player)), 0);

            wordObj.m_nAnswerChoice = -1;
            emit s_Wenti(&wordObj,2,time);
            WaitForUserAction();
            if(CheckGameOver() != Game_Status_Continue)return false;


            //msleep(5000);
            //50%的几率选对
            if(qrand() % 2 == 0)
                wordObj.m_nAnswerChoice = wordObj.answer();
            else
                wordObj.m_nAnswerChoice = qrand() % 4;

        }
        answer = (wordObj.answerChoice() == wordObj.answer());
        emit s_Debug(tr("选择第%1个答案").arg(wordObj.m_nAnswerChoice));

        //再显示一遍
        //if(m_arrayPlayer[player].IsMe())
        {
            //msleep(1000);
            emit s_Wenti(&wordObj,0,0);
            //if(!m_arrayPlayer[player].isMe())
            WaitForUserAction();
            if(CheckGameOver() != Game_Status_Continue)return false;
        }

        //如果是 我 回答单词 并且 单词完结
        if(m_arrayPlayer[player].isMe())
        {
            if(!m_pWordLib->moveToNextWord())
                setGameStatus(Game_Status_GameOver);
        }
    }
    else    //从主机接受问题信息
    {
        qDebug()<<"从主机接受问题中";
        _GameData *pData = (_GameData*)m_Producer.safeOut();
        qDebug()<<"从主机接受问题ok";
        if(CheckGameOver() != Game_Status_Continue)
        {
            if(pData != nullptr)delete pData;
            return false;
        }

        if(pData == nullptr || pData->type != GAME_DATA_TRANSFER_WENTI)
        {
            if(pData != nullptr)
            {
                emit s_Critical(_T("[!GameCore]问题同步 ERROR:%1!").arg(pData->type));
                delete pData;
            }
            else
                emit s_Critical(_T("[!GameCore]问题同步 ERROR:nullptr!"));
            setGameStatus(Game_Status_ERROR);
            return false;
        }


        m_pWordLib->createWord(&wordObj, pData->WenTiData.nAnswer, pData->WenTiData.arrayID);
        delete pData;


        //选择
        if(m_arrayPlayer[player].isMe())
        {
            ShowChuPaiMessage(msg + _T("，请选择一个正确答案 "), 0);

            wordObj.m_nAnswerChoice = -1;
            emit s_Wenti(&wordObj,1,time);
            //RefreshUI();
            WaitForUserAction();
            if(CheckGameOver() != Game_Status_Continue)return false;

            //统计单词和对错
            if(wordObj.answerChoice() == wordObj.answer())
            {
                ContinualChoice(1,wordObj.choiceTime());
            }
            else
            {
                ContinualChoice(0,0);
            }


            QByteArray ba;
            QDataStream out(&ba,QIODevice::WriteOnly);
            out<<(qint16)GAME_DATA<<(qint16)GAME_DATA_TRANSFER_ANSWER
                            <<(qint16)(wordObj.m_nAnswerChoice);

            SendDataToServer(ba);
        }
        else
        {
            ShowChuPaiMessage(msg + _T("，%1正在选择答案").arg(getPlayerShowName(player)), 0);

            wordObj.m_nAnswerChoice = -1;
            emit s_Wenti(&wordObj,3,time);


            emit s_Debug(_T("等待对方选择答案"));
            pData = (_GameData*)m_Producer.safeOut();
            emit s_Debug(_T("接受对方选择答案OK"));
            if(CheckGameOver() != Game_Status_Continue)
            {
                if(pData != nullptr)delete pData;
                return false;
            }

            if(pData == nullptr || pData->type != GAME_DATA_TRANSFER_ANSWER)
            {
                if(pData != nullptr)
                {
                    emit s_Critical(_T("[!GameCore]回答同步 ERROR:%1!").arg(pData->type));
                    delete pData;
                }
                else
                    emit s_Critical(_T("[!GameCore]回答同步 ERROR:nullptr!"));
                setGameStatus(Game_Status_ERROR);
                return false;
            }


            wordObj.m_nAnswerChoice = pData->nAnswer;
            delete pData;
            emit s_Debug(_T("对方选择答案OK"));
        }

        answer = (wordObj.answerChoice() == wordObj.answer());
        emit s_Debug(tr("选择第%1个答案").arg(wordObj.m_nAnswerChoice));

        //再显示一遍
        //if(m_arrayPlayer[player].IsMe())
        {
            //msleep(1000);
            emit s_Wenti(&wordObj,0,0);
            //if(!m_arrayPlayer[player].isMe())
            WaitForUserAction();
            if(CheckGameOver() != Game_Status_Continue)return false;
        }
    }

    //回答结束,显示结果
    msg = _T("提问%1单词：%2")
            .arg(getPlayerShowName(player))
            .arg(wordObj.word());
    emit s_Message(msg);

    msg = _T("%1回答：")
            .arg(getPlayerShowName(player));
    if(wordObj.m_nAnswerChoice == -1)
        msg.append(_T("[不知道]"));
    else
        msg.append(wordObj.explain().at(wordObj.m_nAnswerChoice));
    emit s_Message(msg);

    //qDebug()<<m_pWordLib->result;

    return answer;
}


void Game::SendDataToServer(QByteArray& data, quint8 outControlbyte)
{
    //m_pGameSocket->send();

    if(CheckGameOver() == Game_Status_Continue
            || CheckGameOver() == Game_Status_GameOver)
        emit s_SendDataToServer(data, outControlbyte);
}

void Game::sl_qml_ButtonClicked(int button)
{
    //超时，选择Giveup或Cancel
    if(button ==  -1)
    {
        if(m_flagsButton[Game_Button_Giveup] ==  _BaseObject::Flag_Enable)
        {
            _ClickButton_(Game_Button_Giveup);
        }
        else if(m_flagsButton[Game_Button_Cancel] ==  _BaseObject::Flag_Enable)
        {
            _ClickButton_(Game_Button_Cancel);
        }
        else
        {
            if(m_bNetPlay)
            {
                emit s_Debug("服务器超时");
                return;
            }

            setGameStatus(Game_Status_ERROR);
            emit s_Critical(_T("[!GameCore]TimeUp ERROR!"));
        }

        //qDebug()<<this->iType<<","<<this->iPositionPlayerID<<",ID:"<<this->iID << "zvalue:" << zValue();
        //if(event->button() == Qt::LeftButton)
        //    QMessageBox::information(nullptr,"aa","aa");
        //emit s_ClickPai(m_DianJiPai);
        sl_UserActionFinished();
        return;
    }

    //if(mouseButton == Qt::LeftButton && (this->iFlag & GAME_POKE_FLAG_ENABLE))
    if(m_flagsButton[button] ==  _BaseObject::Flag_Enable)
    {

        _ClickButton_(button);

        //qDebug()<<this->iType<<","<<this->iPositionPlayerID<<",ID:"<<this->iID << "zvalue:" << zValue();
        //if(event->button() == Qt::LeftButton)
        //    QMessageBox::information(nullptr,"aa","aa");
        //emit s_ClickPai(m_DianJiPai);
       sl_UserActionFinished();
    }
}

void Game::sl_qml_DebugButton(int button, QVariant v)
{
    if(button == 10)
    {
        emit s_Debug(_T("牌堆:"));
        for(int i = 0; i < m_nPaiDuiShu; i++)
        {
            emit s_Debug(_T("%1(%2)(%3,%4,%5),")
                  .arg(m_arrayPaiDui[i]->id())
                  .arg(m_arrayPaiDui[i]->getName())
                  .arg(m_arrayPaiDui[i]->positionArea())
                  .arg(m_arrayPaiDui[i]->positionValue())
                  .arg(m_arrayPaiDui[i]->positionPlayer())
                  );
        }
        return;
    }
    else if(button == 11)
    {
        emit s_Debug(_T("弃牌堆:"));
        for(int i = 0; i < m_nQiPaiDuiShu; i++)
        {
            emit s_Debug(_T("%1(%2)(%3,%4,%5),")
                  .arg(m_arrayQiPaiDui[i]->id())
                  .arg(m_arrayQiPaiDui[i]->getName())
                  .arg(m_arrayQiPaiDui[i]->positionArea())
                  .arg(m_arrayQiPaiDui[i]->positionValue())
                  .arg(m_arrayQiPaiDui[i]->positionPlayer())
                  );
        }
        return;
    }

    //
    else if(button == 12)
    {
        emit s_Debug(_T("Player%1手牌").arg(v.toInt()));
        for(int i = 0; i < m_arrayPlayer[v.toInt()].m_listShouPai.length(); i++)
        {
            emit s_Debug(_T("%1(%2)(%3,%4,%5),")
                  .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listShouPai[i])->id())
                  .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listShouPai[i])->getName())
                    .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listShouPai[i])->positionArea())
                    .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listShouPai[i])->positionValue())
                    .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listShouPai[i])->positionPlayer())
                    );
        }
        emit s_Debug(_T("Player%1装备").arg(v.toInt()));
        for(int i = 0; i < m_arrayPlayer[v.toInt()].m_listZhuangBei.length(); i++)
        {
            emit s_Debug(_T("%1(%2)(%3,%4,%5),")
                  .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listZhuangBei[i])->id())
                  .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listZhuangBei[i])->getName())
                    .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listZhuangBei[i])->positionArea())
                    .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listZhuangBei[i])->positionValue())
                    .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listZhuangBei[i])->positionPlayer())
                    );
        }
        emit s_Debug(_T("Player%1判定").arg(v.toInt()));
        for(int i = 0; i < m_arrayPlayer[v.toInt()].m_listPanDing.length(); i++)
        {
            emit s_Debug(_T("%1(%2)(%3,%4,%5),")
                  .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listPanDing[i])->id())
                    .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listPanDing[i])->getName())
                    .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listPanDing[i])->positionArea())
                    .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listPanDing[i])->positionValue())
                    .arg(((_Pai*)m_arrayPlayer[v.toInt()].m_listPanDing[i])->positionPlayer())
                    );
        }
        return;
    }

    //刷新所有牌Flag
    else if(button == 14)
    {
        emit s_RefreshAllFlags();
        return;
    }

    //得到牌
    else if(button == 15)
    {
        int i;
        for(i = 0; i < m_nPaiDuiShu; i++)
        {
            if(v.toInt() == m_arrayPaiDui[i]->value())
            {
                m_arrayPlayer[m_nMyID].getShouPai(m_arrayPaiDui[i]);
                for(; i < m_nPaiDuiShu; i++)
                {
                    m_arrayPaiDui[i] = m_arrayPaiDui[i + 1];
                }
                m_nPaiDuiShu--;
                break;
            }
        }
        for(i = 0; i < m_nQiPaiDuiShu; i++)
        {
            if(v.toInt() == m_arrayQiPaiDui[i]->value())
            {
                m_arrayPlayer[m_nMyID].getShouPai(m_arrayQiPaiDui[i]);
                for(; i < m_nQiPaiDuiShu; i++)
                {
                    m_arrayQiPaiDui[i] = m_arrayQiPaiDui[i + 1];
                }
                m_nQiPaiDuiShu--;
                break;
            }
        }
        emit s_RefreshAllPokePosition();
        emit s_RefreshAllFlags();

    }

    else if(button == 20)       //结束游戏
    {
        //TerminateGame(-1);
        return;
    }

    else if(button == 30)
    {
        m_pGameSocket->close();
    }

    else if(button == 40)
    {
        m_nGameMessageDelay = 200;
    }
    return;
}
